<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="<%=request.getContextPath()%>/">
    <link rel="stylesheet" href="static/plugins/layui/css/layui.css">
    <script src="static/plugins/jquery-3.1.0.js"></script>
    <script src="static/plugins/layui/layui.js"></script>
    <style>
        .required {
            color: red;
            margin: 0 5px;
        }
    </style>
</head>
<body style="background-color: #eee;padding: 15px">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" id="dataForm" lay-filter="dataForm">
            <input type="hidden" name="applyId" id="applyId">
            <div class="layui-form-item">
                <label for="applyUserinfoId" class="layui-form-label">申请人</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="applyUserinfoId" id="applyUserinfoId" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="timeRange" class="layui-form-label">申请时间</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="timeRange" id="timeRange" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="applyReason" class="layui-form-label"><span class="required">*</span>用车事由</label>
                <div class="layui-input-block">
                    <textarea name="applyReason" id="applyReason" class="layui-textarea" cols="30" rows="6"
                              disabled></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="applyCarId" class="layui-form-label">
                    申请车辆
                </label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="applyCarId" id="applyCarId" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="applyType" class="layui-form-label">
                    是否自驾
                </label>
                <div class="layui-input-block">
                    <select lay-filter="applyType" name="applyType" disabled id="applyType">
                        <option value="1">自驾</option>
                        <option value="2">代驾</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item" id="applyDriverIdDiv">
                <label for="applyDriverId" class="layui-form-label">
                    申请司机
                </label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="applyDriverId" id="applyDriverId" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="examDepartmentContent" class="layui-form-label">审核内容</label>
                <div class="layui-input-block">
                    <textarea name="examDepartmentContent" id="examDepartmentContent" cols="30" rows="6"
                              class="layui-textarea"
                              placeholder="同意可以为空，拒绝请输入理由"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <button class="layui-btn" type="submit" lay-filter="agree" lay-submit>同意</button>
                    <button class="layui-btn layui-bg-orange" type="submit" lay-filter="refuse" lay-submit>拒绝</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    let form;
    layui.use(function () {
        form = layui.form;
        getApplyInfo();
        /*监听表单提交事件*/
        form.on("submit(agree)", function (obj) {
            const formData = {}
            for (let o of obj.form) {
                if (o.id && !o.disabled) {
                    let k = $(o).prop("name");
                    formData[k] = $(o).val();
                }
            }
            formData.applyStatus = 2;
            if (!formData.examDepartmentContent) {
                formData.examDepartmentContent = '同意';
            }
            exam(formData);
            return false;
        });
        form.on("submit(refuse)", function (obj) {
            const formData = {}
            for (let o of obj.form) {
                if (o.id && !o.disabled) {
                    let k = $(o).prop("name");
                    formData[k] = $(o).val();
                }
            }
            formData.applyStatus = 3;
            if (!formData.examDepartmentContent) {
                formData.examDepartmentContent = '拒绝';
            }
            exam(formData);
            return false;
        });
    });

    function exam(data) {
        $.ajax({
            url: "apply/exam",
            method: "post",
            data,
            dataType: "json",
            success(res) {
                if (res.code === 200) {
                    layer.msg('审核完毕', {icon: 6}, function () {
                        let index = parent.layer.getFrameIndex(window.name); // 先得到当前 iframe 层的索引
                        parent.layer.close(index); // 再执行关闭
                        /*刷新list页面的table*/
                        parent.table.reload('dataTable');
                    });
                } else {
                    layer.alert(res.msg, {icon: 5});
                }
            }
        });
    }

    function getApplyInfo() {
        const dataId = sessionStorage.getItem("dataId");
        $.ajax({
            url: "apply/info/" + dataId,
            method: "get",
            dataType: "json",
            success(res) {
                if (res.code === 200) {
                    if (res.data.applyType === 1) {
                        $("#applyDriverIdDiv").hide();
                    } else {
                        $("#applyDriverIdDiv").show();
                    }
                    const formData = {
                        ...res.data,
                        applyUserinfoId: res.data.applyUserinfo.nickname + '-[' + res.data.applyUserinfo.department.name + ']',
                        applyCarId: res.data?.applyCar?.carNo ?? '未指定',
                        applyDriverId: res.data?.applyDriver?.nickname ?? '未指定',
                        timeRange: res.data.applyStartTime + ' - ' + res.data.applyEndTime,
                    }
                    form.val('dataForm', formData);
                }
            }
        });
        sessionStorage.removeItem("dataId");
    }

</script>
</html>
